/* VatCheck.java
 * 6 June 2012
 */
package za.ac.wits.elen7045.integrity.checks;

import java.util.ArrayList;
import java.util.List;

import jass.modern.Post;
import jass.modern.Pre;
import za.ac.wits.elen7045.integrity.checks.interfaces.IntegrityCheck;
import za.ac.wits.elen7045.integrity.checks.interfaces.IntegrityCheckResult;
import za.ac.wits.elen7045.scraper.interfaces.ScraperSessionInterface;

/**
 * Calculates the VAT using a configurable VAT rate, and the before and agter VAT balances.
 * 
 * @author Gerhard Visagie
 *
 */
public class VatCheck implements IntegrityCheck {

	/**
	 * Does a check to see if the included and excluding vat values are the same
	 * Preconditions:
	 *  scraperSession data pairs must contain a including vat value
	 *  scraperSession data pairs must contain a excluding vat value
	 *  
	 * @see za.ac.wits.elen7045.integrity.checks.interfaces.IntegrityCheck#doCheck(za.ac.wits.elen7045.scraper.interfaces.ScraperSessionInterface)
	 */
	@Override
	@Pre("scraperSession != null "+
			"&& scraperSession.get(\"Total exl Vat\") != null " +
			"&& scraperSession.get(\"Total Due\") != null")
	@Post("@Result == true || @Result == false")
	public StandardIntegrityCheckReport doCheck(ScraperSessionInterface scraperSession) {
		// TODO get vat rate from properties
		// TODO get datapair names from a configurable places
		double beforeVat = Double.parseDouble(scraperSession.get("Total exl Vat").getValue());
		double withVat = Double.parseDouble(scraperSession.get("Total Due").getValue());
		double vatRate = 14;
		double calcedWithVat = (beforeVat * (vatRate/100))+beforeVat;
		List<IntegrityCheckResult> result = new ArrayList<IntegrityCheckResult>();
		result.add(new StandardIntegrityCheckResult("Total Due",withVat+"","Total Due,",(Math.round(calcedWithVat*100.0) / 100.0)+""));
		if((Math.round(calcedWithVat*100.0) / 100.0) == withVat){
			return new StandardIntegrityCheckReport(true, result);
		}else{
			return new StandardIntegrityCheckReport(false, result);
		}
	}

}
